﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Business
{
    public class DonDatNCC
    {
        
        public int MaDonDatNCC { get; set; }
        public string MaDonDatNCCHienThi
        {
            get
            {
                return "DDNCC" + MaDonDatNCC;
            }
        }
        public DateTime NgayDat { get; set; }
        public DateTime NgayGiao { get; set; }

        public double SoTien { get; set; }
        public string GhiChu { get; set; }
        public List<ChiTietDonDatNCC> DanhSachChiTietDonDat { get; set; }
        

        
        public DonDatNCC()
        {
            this.MaDonDatNCC = 0;
            DanhSachChiTietDonDat = new List<ChiTietDonDatNCC>();
        }

        public DonDatNCC(int maDDH, DateTime ngayDat, DateTime ngayGiao, double soTien, string ghiChu)
        {
            this.MaDonDatNCC = maDDH;
            this.NgayDat = ngayDat;
            this.NgayGiao = ngayGiao;
            this.SoTien = soTien;
            this.GhiChu = ghiChu;
        }
        

      

        public void ThemMoiChiTietDonDat(int maDDH, int maHang,
            string tenHang, int soLuong, double giaNhap)
        {
            ChiTietDonDatNCC chiTietDonDat = new ChiTietDonDatNCC(maDDH,
                maHang, tenHang, soLuong, giaNhap);

            DanhSachChiTietDonDat.Add(chiTietDonDat);
        }

        public void CapNhatChiTietDonDat(int index, int maDDH, int maHang,
            string tenHang, int soLuong, double giaNhap)
        {
            ChiTietDonDatNCC chiTietDonDat = DanhSachChiTietDonDat[index];
            chiTietDonDat.CapNhat(maHang, tenHang, soLuong, giaNhap);
        }

        public void XoaChiTietDonDat(int index)
        {
            DanhSachChiTietDonDat.RemoveAt(index);
        }

        private bool TonTaiChiTietDonDat(int maCTDDH)
        {
            foreach (var chitietDonDat in this.DanhSachChiTietDonDat)
            {
                if (chitietDonDat.MaCTDDH == maCTDDH)
                    return true;
            }
            return false;
        }

        public double TinhTongTienHang()
        {
            double tongTien = 0;
            foreach (var chiTietDonDat in DanhSachChiTietDonDat)
            {
                tongTien += chiTietDonDat.SoLuong * chiTietDonDat.GiaNhap;
            }
            return tongTien;
        }
        

      
        public static int ThemMoi(DonDatNCC nhapObject)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

            DataAccess.DonDatNCC tamObject = new DataAccess.DonDatNCC();
            tamObject.NgayDat = nhapObject.NgayDat;
            tamObject.NgayGiao = nhapObject.NgayGiao;
            tamObject.TongGT = nhapObject.TinhTongTienHang();
            tamObject.GhiChu = nhapObject.GhiChu;
            db.DonDatNCCs.InsertOnSubmit(tamObject);
            db.SubmitChanges();

            foreach (var chitietDonDat in nhapObject.DanhSachChiTietDonDat)
            {
                DataAccess.ChiTietDonDatNCC chiTietTamObject = new DataAccess.ChiTietDonDatNCC();
                chiTietTamObject.MaDonDatNCC = tamObject.MaDonDatNCC;
                chiTietTamObject.MaHang = chitietDonDat.MaHang;
                chiTietTamObject.GiaNhap = chitietDonDat.GiaNhap;
                chiTietTamObject.SoLuong = chitietDonDat.SoLuong;

                db.ChiTietDonDatNCCs.InsertOnSubmit(chiTietTamObject);
            }

            db.SubmitChanges();

            return tamObject.MaDonDatNCC;
        }



        public static DonDatNCC LayDonDatNCC(int maDonDat)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
            DonDatNCC donDatHang = (from p in db.DonDatNCCs

                                    where p.MaDonDatNCC == maDonDat
                                    select new DonDatNCC(p.MaDonDatNCC, p.NgayDat, p.NgayGiao, (double)p.TongGT, p.GhiChu)).Single();

            List<ChiTietDonDatNCC> danhsachChiTietDonDat = (from p in db.ChiTietDonDatNCCs
                                                            where p.MaDonDatNCC == maDonDat
                                                            select new ChiTietDonDatNCC(p.MaCTDDNCC, p.MaDonDatNCC, p.MaHang, p.HangHoa.TenHang, p.SoLuong, p.GiaNhap)).ToList();
            if (danhsachChiTietDonDat == null || danhsachChiTietDonDat.Count == 0)

                danhsachChiTietDonDat = new List<ChiTietDonDatNCC>();

            donDatHang.DanhSachChiTietDonDat = danhsachChiTietDonDat;

            return donDatHang;
        }

        public static void CapNhat(DonDatNCC nhapObject)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

            
            var tamObject = (from p in db.DonDatNCCs
                             where p.MaDonDatNCC == nhapObject.MaDonDatNCC
                             select p).Single();
            tamObject.NgayDat = nhapObject.NgayDat;
            tamObject.NgayGiao = nhapObject.NgayGiao;
            tamObject.TongGT = nhapObject.TinhTongTienHang();
            tamObject.GhiChu = nhapObject.GhiChu;

            
            foreach (var chiTietTamObject in tamObject.ChiTietDonDatNCCs)
            {
                if (!nhapObject.TonTaiChiTietDonDat(chiTietTamObject.MaCTDDNCC))
                    db.ChiTietDonDatNCCs.DeleteOnSubmit(chiTietTamObject);
            }


            
            foreach (var chitietDonDat in nhapObject.DanhSachChiTietDonDat)
            {
                if (chitietDonDat.MaCTDDH == 0)
                {
                    DataAccess.ChiTietDonDatNCC chiTietTamObject = new DataAccess.ChiTietDonDatNCC();
                    chiTietTamObject.MaDonDatNCC = nhapObject.MaDonDatNCC;
                    chiTietTamObject.MaHang = chitietDonDat.MaHang;
                    chiTietTamObject.GiaNhap = chitietDonDat.GiaNhap;
                    chiTietTamObject.SoLuong = chitietDonDat.SoLuong;

                    db.ChiTietDonDatNCCs.InsertOnSubmit(chiTietTamObject);
                }
                else
                {
                    var chiTietTamObject = (from p in db.ChiTietDonDatNCCs
                                            where p.MaCTDDNCC == chitietDonDat.MaCTDDH
                                            select p).Single();
                    chiTietTamObject.MaHang = chitietDonDat.MaHang;
                    chiTietTamObject.GiaNhap = chitietDonDat.GiaNhap;
                    chiTietTamObject.SoLuong = chitietDonDat.SoLuong;
                }
            }

            db.SubmitChanges();
        }

        public static List<DonDatNCC> LayDanhSach_MaDonDat(string maDDHT)
        {
            try
            {


                int maDDH = Int16.Parse(maDDHT.Substring(5));
                DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

                return (from p in db.DonDatNCCs
                        where p.MaDonDatNCC == maDDH

                        select new DonDatNCC(p.MaDonDatNCC, p.NgayDat, p.NgayGiao, p.TongGT, p.GhiChu)).ToList();
            }
            catch
            {
                return LayDanhSach();
            }
        }
        public static void Xoa(int maDDH)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

            var xoaObject = (from p in db.DonDatNCCs
                             where p.MaDonDatNCC == maDDH
                             select p).Single();

            db.DonDatNCCs.DeleteOnSubmit(xoaObject);

            db.SubmitChanges();
        }

      

        public static List<DonDatNCC> LayDanhSach()
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
            return (from p in db.DonDatNCCs

                    select new DonDatNCC(p.MaDonDatNCC, p.NgayDat, p.NgayGiao, p.TongGT, p.GhiChu)).ToList();
        }

        public static List<DonDatNCC> LayDanhSach_NgayDat(DateTime tungay, DateTime denngay)
        {
            try
            {
                DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
                return (from p in db.DonDatNCCs

                        where p.NgayDat.Date >= tungay.Date && p.NgayDat.Date <= denngay.Date
                        select new DonDatNCC(p.MaDonDatNCC, p.NgayDat, p.NgayGiao, p.TongGT, p.GhiChu)).ToList();
            }
            catch 
            {
                return LayDanhSach();
            }
        }

        public static List<DonDatNCC> LayDanhSach_NgayGiao(DateTime tungay, DateTime denngay)
        {
            try
            {
                DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
                return (from p in db.DonDatNCCs

                        where p.NgayGiao.Date >= tungay.Date && p.NgayGiao.Date <= denngay.Date
                        select new DonDatNCC(p.MaDonDatNCC, p.NgayDat, p.NgayGiao, p.TongGT, p.GhiChu)).ToList();
            }
            catch 
            {
                return LayDanhSach();
            }
        }

      
     
    }
}
